import encodings
import json
import requests
import logging

logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s %(name)s %(levelname)s %(pathname)s %(lineno)s %(funcName)s - %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S')
# 定义 API 端点和 API Key
base_url = "http://172.18.80.154/v1/"
api_key = "app-nRTPgTqwpOQ37EgVd1XofL4K"  # 替换为你的 API Key


# 发送对话消息
def get_message(message):
    url = f"{base_url}chat-messages"
    # 定义请求头
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json; charset=utf-8",
    }

    # 定义请求体
    data = {
        "inputs": {},
        # "query": "What are the specs of the iPhone 13 Pro Max?",
        "query": f"{message}",
        "response_mode": "streaming",
        "conversation_id": "",
        "user": "abc-123",
        # "files": [
        #     {
        #         "type": "image",
        #         "transfer_method": "remote_url",
        #         "url": "https://cloud.dify.ai/logo/logo-site.png",
        #     }
        # ],
    }

    # 将请求体转换为 JSON 字符串并确保 UTF-8 编码
    json_data = json.dumps(data, ensure_ascii=False).encode("utf-8")

    # 发送 POST 请求并处理流式响应
    with requests.post(url, headers=headers, data=json_data, stream=True) as response:
        if response.status_code == 200:
            print("请求成功！")
            for chunk in response.iter_content(chunk_size=None):
                if chunk:
                    # 确保流式数据使用 UTF-8 解码
                    # print("收到数据块：", chunk.decode("utf-8"))
                    try:
                        print("收到数据块1：", chunk.decode("unicode_escape"))
                    except UnicodeDecodeError:
                        print("收到数据块2：", chunk.decode("utf-8"))
        else:
            print(f"请求失败，状态码：{response.status_code}")
            # 确保错误信息使用 UTF-8 解码
            error_text = response.content.decode("utf-8")
            print("错误信息：", error_text)


# 获取服务器信息
def get_info():
    url = f"{base_url}info"
    # 定义请求头
    headers = {
        "Authorization": f"Bearer {api_key}",
    }
    response = requests.get(url, headers=headers)
    if response.status_code == 200:
        logging.info("响应内容：{}".format(response.text.encode("utf-8").decode("unicode_escape")))


def get_parameters():
    url = f"{base_url}parameters"
    # 定义请求头
    headers = {
        "Authorization": f"Bearer {api_key}",
    }
    response = requests.get(url, headers=headers)
    if response.status_code == 200:
        decode = response.text.encode("utf-8").decode("unicode_escape")
        logging.info("响应内容：{}".format(decode))
        print(json.loads(decode))
        # print(response.text)
        # response_text = response.content.encode("utf-8").decode("unicode_escape")
        # print("响应内容：", response_text)


def get_meta():
    url = f"{base_url}meta"
    # 定义请求头
    headers = {
        "Authorization": f"Bearer {api_key}",
    }
    response = requests.get(url, headers=headers)
    if response.status_code == 200:
        logging.info("响应内容：{}".format(response.text.encode("utf-8").decode("unicode_escape")))

        # response_text = response.content.decode("utf-8")
        # print("响应内容：", response_text)
        # print(response.text)


if __name__ == '__main__':
    str=r'{"event": "message_end", "conversation_id": "fb59f804-8420-44a5-96e0-3d014475e4c9", "message_id": "b662e129-c681-4448-b6d0-b8b65b517287", "created_at": 1742374876, "task_id": "136d3d24-9910-4002-a479-eb3b2151efcf", "id": "b662e129-c681-4448-b6d0-b8b65b517287", "metadata": {"retriever_resources": [{"dataset_id": "1a1cc6e4-1f3e-4243-9799-ddf03fada4c3", "dataset_name": "\u52ff\u52a8-\u9879\u76ee\u6e05\u5355.txt...", "document_id": "11e050e8-9a5d-4d31-ac28-32f4de921233", "document_name": "\u9879\u76ee\u6e05\u5355.txt", "data_source_type": "upload_file", "segment_id": "abde92d6-746c-42d1-8de9-b085aff2db09", "retriever_from": "api", "score": 0.5820868583140836, "doc_metadata": null, "content": "\n\u540d\u79f0: \u6c5f\u897f\u79d1\u6668\u7814\u53d1\u4e2d\u5fc3\n\u4ee3\u7801: 2103-360111-04-05-561667\n\u7acb\u9879\u7c7b\u578b: \u5907\u6848\n\u9879\u76ee\u7c7b\u578b: \u793e\u4f1a\u6295\u8d44\u623f\u5c4b\u5efa\u7b51\u9879\u76ee\n\u5efa\u8bbe\u6027\u8d28: \u65b0\u5efa\n\u7528\u5730\u9762\u79ef\uff08\u5e73\u65b9\u7c73\uff09: 13817.66\n\u5efa\u7b51\u9762\u79ef\uff08\u5e73\u65b9\u7c73\uff09: 67210.45\n\u603b\u6295\u8d44\u989d\uff08\u4e07\u5143\uff09: 250000\n\u5de5\u7a0b\u5206\u7c7b: \n\u5efa\u8bbe\u5730\u5740: \u6c5f\u897f\u7701\u5357\u660c\u5e02\u9752\u5c71\u6e56\u533a\u6c5f\u897f\u7701\u5357\u660c\u5e02\u9752\u5c71\u6e56\u533a\u9752\u5c71\u6e56\u5317\u5927\u9053\u4ee5\u4e1c\u3001\u6c11\u5f3a\u8def\u4ee5\u5357\n\u5efa\u8bbe\u89c4\u6a21\u53ca\u5185\u5bb9: \u5efa\u7b51\u9762\u79ef\uff1a67210.45\u5e73\u65b9\u7c73 \u7528\u5730\u9762\u79ef\uff1a13817.66\u5e73\u65b9\u7c73 \u5176\u4ed6\uff1a\u8be5\u9879\u76ee\u4f4d\u4e8e\u6c5f\u897f\u7701\u5357\u660c\u5e02\u9752\u5c71\u6e56\u533a\u5858\u5c71\u9547\u9752\u5c71\u6e56\u5317\u5927\u9053\u4ee5\u4e1c\u3001\u6c11\u5f3a\u8def\u4ee5\u5357\u3001\u89c4\u5212\u8def\u4ee5\u897f\uff0c\u7b79\u5efa\u7535\u529b\u4ea7\u4e1a\u603b\u90e8\u5927\u697c\u3002\u8be5\u9879\u76ee\u4e3b\u8981\u56f4\u7ed5\u4fe1\u606f\u7cfb\u7edf\u8fd0\u7ef4\u3001\u9020\u4ef7\u54a8\u8be2\u3001\u667a\u6167\u7269\u6d41\u3001\u5145\u7535\u8bbe\u65bd\u5efa\u8bbe\u3001\u79d1\u6280\u6210\u679c\u5b75\u5316\u3001\u5206\u5e03\u5f0f\u50a8\u80fd\u3001\u8d44\u91d1\u8d44\u4ea7\u8fd0\u8425\u7b49\u65b9\u9762\u6269\u5927\u65b0\u578b\u7ecf\u8425\u7b49\u4e1a\u52a1\u3002\n", "position": 1}, {"dataset_id": "1a1cc6e4-1f3e-4243-9799-ddf03fada4c3", "dataset_name": "\u52ff\u52a8-\u9879\u76ee\u6e05\u5355.txt...", "document_id": "11e050e8-9a5d-4d31-ac28-32f4de921233", "document_name": "\u9879\u76ee\u6e05\u5355.txt", "data_source_type": "upload_file", "segment_id": "4bfeb03d-1e33-4c01-8253-2552657bb3b9", "retriever_from": "api", "score": 0.43238177175851183, "doc_metadata": null, "content": "\n\u540d\u79f0: \u5357\u660c\u5e02\u9752\u5c71\u6e56\u533a\u4e49\u574a\u5b89\u7f6e\u5c0f\u533a\u57ce\u5e02\u68da\u6237\u533a\u6539\u9020\u5b89\u7f6e\u623f\u5efa\u8bbe\u9879\u76ee\n\u4ee3\u7801: 2018-360100-47-01-008042\n\u7acb\u9879\u7c7b\u578b: \u5ba1\u6279\n\u9879\u76ee\u7c7b\u578b: \u653f\u5e9c\u7c7b\u623f\u5c4b\u5efa\u7b51[\u6539\u5efa]\n\u5efa\u8bbe\u6027\u8d28: \u65b0\u5efa\n\u7528\u5730\u9762\u79ef\uff08\u5e73\u65b9\u7c73\uff09: 116855.33\n\u5efa\u7b51\u9762\u79ef\uff08\u5e73\u65b9\u7c73\uff09: 346707.12\n\u603b\u6295\u8d44\u989d\uff08\u4e07\u5143\uff09: 145500\n\u5de5\u7a0b\u5206\u7c7b: \n\u5efa\u8bbe\u5730\u5740: \u6c5f\u897f\u7701\u5357\u660c\u5e02\u9752\u5c71\u6e56\u533a\u4e1c\u6cf0\u5927\u9053\u4ee5\u4e1c\uff0c\u660c\u5bcc\u8def\u4ee5\u5357\uff0c\u660c\u4e1c\u5927\u9053\u4ee5\u897f\uff0c\u5e7f\u5dde\u8def\u4ee5\u5317\n\u5efa\u8bbe\u89c4\u6a21\u53ca\u5185\u5bb9: \u5efa\u7b51\u9762\u79ef\uff1a346707.12\u5e73\u65b9\u7c73 \u7528\u5730\u9762\u79ef\uff1a116855.33\u5e73\u65b9\u7c73 \n", "position": 2}, {"dataset_id": "1a1cc6e4-1f3e-4243-9799-ddf03fada4c3", "dataset_name": "\u52ff\u52a8-\u9879\u76ee\u6e05\u5355.txt...", "document_id": "11e050e8-9a5d-4d31-ac28-32f4de921233", "document_name": "\u9879\u76ee\u6e05\u5355.txt", "data_source_type": "upload_file", "segment_id": "3b902583-1844-42a2-b8fb-4464d93949f0", "retriever_from": "api", "score": 0.42446371615669976, "doc_metadata": null, "content": "\n\u540d\u79f0: \u6c5f\u897f\u946b\u9f0e\u7eba\u7ec7\u670d\u88c5\u57fa\u5730\u9879\u76ee\n\u4ee3\u7801: 2206-360111-04-01-246147\n\u7acb\u9879\u7c7b\u578b: \u5907\u6848\n\u9879\u76ee\u7c7b\u578b: \u793e\u4f1a\u6295\u8d44\u5de5\u4e1a\u3001\u4ed3\u50a8\u7c7b\u9879\u76ee\n\u5efa\u8bbe\u6027\u8d28: \u65b0\u5efa\n\u7528\u5730\u9762\u79ef\uff08\u5e73\u65b9\u7c73\uff09: 59477\n\u5efa\u7b51\u9762\u79ef\uff08\u5e73\u65b9\u7c73\uff09: 73950.87\n\u603b\u6295\u8d44\u989d\uff08\u4e07\u5143\uff09: 20000\n\u5de5\u7a0b\u5206\u7c7b: \n\u5efa\u8bbe\u5730\u5740: \u6c5f\u897f\u7701\u5357\u660c\u5e02\u9752\u5c71\u6e56\u533a\u660c\u4e1c\u4e09\u8def\u4ee5\u897f\uff0c\u987a\u5916\u5b9e\u4e1a\u6709\u9650\u516c\u53f8\u4ee5\u5317\n\u5efa\u8bbe\u89c4\u6a21\u53ca\u5185\u5bb9: \u5efa\u7b51\u9762\u79ef\uff1a73950.87\u5e73\u65b9\u7c73 \u7528\u5730\u9762\u79ef\uff1a59477.0\u5e73\u65b9\u7c73 \u5176\u4ed6\uff1a\u89c4\u5212\u7528\u5730\u9762\u79ef\uff1a59478.41\u5e73\u65b9\u7c73\uff0c\u603b\u5efa\u7b51\u9762\u79ef\uff1a73950.87\u5e73\u65b9\u7c73\n", "position": 3}, {"dataset_id": "1a1cc6e4-1f3e-4243-9799-ddf03fada4c3", "dataset_name": "\u52ff\u52a8-\u9879\u76ee\u6e05\u5355.txt...", "document_id": "11e050e8-9a5d-4d31-ac28-32f4de921233", "document_name": "\u9879\u76ee\u6e05\u5355.txt", "data_source_type": "upload_file", "segment_id": "522b9c4a-0a09-4602-94a6-84a9fd9059e4", "retriever_from": "api", "score": 0.4239975041213206, "doc_metadata": null, "content": "\n\u540d\u79f0: \u6e56\u7554\u548c\u5149\n\u4ee3\u7801: 2206-360111-04-01-186304\n\u7acb\u9879\u7c7b\u578b: \u5907\u6848\n\u9879\u76ee\u7c7b\u578b: \u793e\u4f1a\u6295\u8d44\u5176\u4ed6\u7c7b\n\u5efa\u8bbe\u6027\u8d28: \u65b0\u5efa\n\u7528\u5730\u9762\u79ef\uff08\u5e73\u65b9\u7c73\uff09: 27760.82\n\u5efa\u7b51\u9762\u79ef\uff08\u5e73\u65b9\u7c73\uff09: 92997.23\n\u603b\u6295\u8d44\u989d\uff08\u4e07\u5143\uff09: 200000\n\u5de5\u7a0b\u5206\u7c7b: \u6c11\u7528\u5efa\u7b51\n\u5efa\u8bbe\u5730\u5740: \u6c5f\u897f\u7701\u5357\u660c\u5e02\u9752\u5c71\u6e56\u533a\u9752\u5c71\u6e56\u533a\u6c11\u56ed\u8def\u4ee5\u5357\u3001\u9ad8\u65b0\u5927\u9053\u4ee5\u897f\uff08\u63a7\u89c4CD201-TK06-01\uff09\n\u5efa\u8bbe\u89c4\u6a21\u53ca\u5185\u5bb9: \u5efa\u7b51\u9762\u79ef\uff1a92997.23\u5e73\u65b9\u7c73 \u7528\u5730\u9762\u79ef\uff1a27760.82\u5e73\u65b9\u7c73 \u5176\u4ed6\uff1a\u8be5\u9879\u76ee\u603b\u7528\u5730\u9762\u79ef 27760.82\u5e73\u65b9\u7c73\uff0c\u603b\u5efa\u7b51\u9762\u79ef\u4e3a92997.23\u5e73\u65b9\u7c73\u3002\u571f\u5730\u6027\u8d28\u4e3a\u4e8c\u7c7b\u57ce\u9547\u4f4f\u5b85\u7528\u5730\uff0c\u5bb9\u79ef\u7387\u22642.5\uff0c\u5efa\u7b51\u5bc6\u5ea6\u226430\uff05\uff0c\u7eff\u5730\u7387\u226530\uff05\uff0c\u8ba1\u5bb9\u9762\u79ef\u4e3a69402.06\u5e73\u65b9\u7c73\uff0c\u4e0d\u8ba1\u5bb9\u9762\u79ef\u4e3a23595.17\u5e73\u65b9\u7c73\uff0c\u75316\u680b\u9ad8\u5c42\u4f4f\u5b85\u30012\u680b\u5546\u4e1a\u53ca\u914d\u5957\u30011\u680b\u5927\u95e8\u53ca\u5730\u4e0b\u5ba4\u7ec4\u6210\u3002\n", "position": 4}], "usage": {"prompt_tokens": 1140, "prompt_unit_price": "0", "prompt_price_unit": "0", "prompt_price": "0", "completion_tokens": 671, "completion_unit_price": "0", "completion_price_unit": "0", "completion_price": "0", "total_tokens": 1811, "total_price": "0", "currency": "USD", "latency": 36.57862868200027}}, "files": null}'
    # str=r'\u53ef\u8bc1\u6838\u53d1\n\u5ba1\u6279\u90e8\u95e8\uff1a\u9752\u4e91\u8c31\u533a\u4f4f\u623f\u548c\u57ce\u4e61\u5efa\u8bbe\u5c40\n##\n\u5ba1\u6279\u9636\u6bb5\uff1a\u5e76\u884c\u63a8\u8fdb\n\u529e\u7406\u4e8b\u9879\u540d\u79f0\uff1a\u4f9b\u7535\u62a5\u88c5\n\u5ba1\u6279\u90e8\u95e8\uff1a\u5357\u660c\u5e02\u4f9b\u7535\u516c\u53f8\n##\n\u5ba1\u6279\u9636\u6bb5\uff1a\u5e76\u884c", "position": 2}, {"dataset_id": "5b0fd708-39f5-4415-afc5-c6021e8cbbfa", "dataset_name": "\u52ff\u52a8-wjq\u529e\u4ef6\u6e05\u5355", "document_id": "95f17d81-379c-4e28-9c25-083b502a8bf4", "document_name": "\u529e\u4ef6\u4fe1\u606f.txt", "data_source_type": "upload_file", "segment_id": "1af4ddfb-4e90-4da6-80d4-f800434e843a", "retriever_from": "api", "score": 0.24591734409332275, "doc_metadata": null, "content": "\u540d\u79f0\uff1a\u4e2d\u7cae\u5927\u60a6\u57ce\n\u4ee3\u7801\uff1a2012-360103-04-01-214454\n##\n\u5ba1\u6279\u9636\u6bb5\uff1a\u5e76\u884c\u63a8\u8fdb\n\u529e\u7406\u4e8b\u9879\u540d\u79f0\uff1a\u4f9b\u7535\u62a5\u88c5\uff08\u542b\u4e34\u65f6\u7528\u7535\uff09\n\u5ba1\u6279\u90e8\u95e8\uff1a\u5357\u660c\u5e02\u4f9b\u7535\u516c\u53f8\n##\n\u5ba1\u6279\u9636\u6bb5\uff1a\u5e76\u884c\u63a8\u8fdb\n\u529e\u7406\u4e8b\u9879\u540d\u79f0\uff1a\u751f\u4ea7\u5efa\u8bbe\u9879\u76ee\u6c34\u571f\u4fdd\u6301\u65b9\u6848\u5ba1\u6279\n\u5ba1\u6279\u90e8\u95e8\uff1a\u897f\u6e56\u533a\u4f4f\u623f\u548c\u57ce\u4e61\u5efa\u8bbe\u5c40\n##\n\u5ba1\u6279\u9636\u6bb5\uff1a\u5e76\u884c\u63a8\u8fdb\n\u529e\u7406\u4e8b\u9879\u540d\u79f0\uff1a\u5efa\u8bbe\u5de5\u7a0b\u6d88\u9632\u8bbe\u8ba1\u5ba1\u67e5\uff08\u623f\u5efa\u3001\u5e02\u653f\u5de5\u7a0b\uff09\n\u5ba1\u6279\u90e8\u95e8\uff1a\u5357\u660c\u5e02\u4f4f\u623f\u548c\u57ce\u4e61\u5efa\u8bbe\u5c40\n##\n\u5ba1\u6279\u9636\u6bb5\uff1a\u5e76\u884c\u63a8\u8fdb\n\u529e\u7406\u4e8b\u9879\u540d\u79f0\uff1a\u7ed3\u5408\u6c11\u7528\u5efa\u7b51\u4fee\u5efa\u9632\u7a7a\u5730\u4e0b\u5ba4\u5efa\u8bbe\u9879\u76ee\u5ba1\u6279\n\u5ba1\u6279\u90e8\u95e8\uff1a\u4eba\u9632\u4ea4\u901a\u5ba1\u6279\u670d\u52a1\u5904\n##\n\u5ba1\u6279\u9636\u6bb5\uff1a\u5e76\u884c\u63a8\u8fdb\n\u529e\u7406\u4e8b\u9879\u540d\u79f0\uff1a\u8d85\u9650\u9ad8\u5c42\u5efa\u7b51\u5de5\u7a0b\u6297\u9707\u8bbe\u9632\u4e13\u9879\u5ba1\u67e5\n\u5ba1\u6279\u90e8\u95e8\uff1a\u5efa\u7b51\u5de5\u7a0b\u5ba1\u6279\u670d\u52a1\u79d1\n##\n\u5ba1\u6279\u9636\u6bb5\uff1a\u5e76\u884c\u63a8\u8fdb\n\u529e\u7406\u4e8b\u9879\u540d\u79f0\uff1a\u5efa\u8bbe\u5de5\u7a0b\u89c4\u5212\u8bb8\u53ef\u8bc1\u6838\u53d1\uff08\u5efa\u8bbe\u5de5\u7a0b\u8bbe\u8ba1\u65b9\u6848\u5ba1\u5b9a\uff09\n\u5ba1\u6279\u90e8\u95e8\uff1a\u5efa\u8bbe\u5de5\u7a0b\u89c4\u5212\u5ba1\u6279\u670d\u52a1\u79d1\n##\n\u5ba1\u6279\u9636\u6bb5\uff1a\u5e76", "position": 3}, {"dataset_id": "1a1cc6e4-1f3e-4243-9799-ddf03fada4c3", "dataset_name": "\u52ff\u52a8-\u9879\u76ee\u6e05\u5355.txt...", "document_id": "11e050e8-9a5d-4d31-ac28-32f4de921233", "document_name": "\u9879\u76ee\u6e05\u5355.txt", "data_source_type": "upload_file", "segment_id": "fb45657b-6190-4345-b22e-c48b89595443", "retriever_from": "api", "score": 0.19800003409385677, "doc_metadata": null, "content": "\n\u540d\u79f0: \n\u4ee3\u7801: \n\u7acb\u9879\u7c7b\u578b: \n\u9879\u76ee\u7c7b\u578b: \n\u5efa\u8bbe\u6027\u8d28: \n\u7528\u5730\u9762\u79ef\uff08\u5e73\u65b9\u7c73\uff09: \n\u5efa\u7b51\u9762\u79ef\uff08\u5e73\u65b9\u7c73\uff09: \n\u603b\u6295\u8d44\u989d\uff08\u4e07\u5143\uff09: \n\u5de5\u7a0b\u5206\u7c7b: \n\u5efa\u8bbe\u5730\u5740: \n\u5efa\u8bbe\u89c4\u6a21\u53ca\u5185\u5bb9: \n", "position": 4}], "usage": {"prompt_tokens": 1122, "prompt_unit_price": "0", "prompt_price_unit": "0", "prompt_price": "0", "completion_tokens": 162, "completion_unit_price": "0", "completion_price_unit": "0", "completion_price": "0", "total_tokens": 1284, "total_price": "0", "currency": "USD", "latency": 10.723224637000385}}, "files": null}'
    print(str.encode("utf-8").decode("unicode_escape"))
    # get_message('我想在江西省南昌市青山湖区建一个小区，用地面积：约14000平方米，建筑面积：约50000平方米，总投资额：约500000万元，请问我要办理哪些事项?')
    # get_info()
    # get_parameters()
    # get_meta()
    pass
